//
//  VIZWaringController.m
//  Young-Old
//
//  Created by 位立志 on 16/4/28.
//  Copyright © 2016年 位立志. All rights reserved.
//

#import "VIZWaringController.h"

@interface VIZWaringController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSMutableArray *warns;

@end

@implementation VIZWaringController

static NSString *ID = @"warn";

//懒加载warns
- (NSMutableArray *)warns{
    
    if (_warns == nil) {
//        _warns = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getFilePath]];
//        if (_warns == nil) {
            _warns = [NSMutableArray array];
        //}
    }
    
    return _warns;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUI];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    self.tableView.rowHeight = 100;
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"View" bundle:nil] forCellReuseIdentifier:ID];

}

#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.warns.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    VIZWarnsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    cell.warn = self.warns[indexPath.row];

    return cell;
}






- (void)setUI{
    [self setTitle:@"编辑提醒"];
    
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnClick)];
    [self.navigationItem setLeftBarButtonItem:leftBtn];
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightBtnClick)];
    [self.navigationItem setRightBarButtonItem:rightBtn];
}

//左边编辑按钮(归档来存储提醒)
- (void)leftBtnClick{
    NSLog(@"左边编辑按钮");
}

//右边添加按钮
- (void)rightBtnClick{
    NSLog(@"右边按钮点击");
    
    VIZAddController *addVc = [[VIZAddController alloc]init];
    addVc.plist = @"AddClock";
    VIZNavgationController *addNav = [[VIZNavgationController alloc]initWithRootViewController:addVc];
    [self presentViewController:addNav animated:YES completion:nil];
    
    
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                         @"12:00", @"time",
                         @"hahahahha", @"tip",
                         @"每天",@"repeat",
                         @"雷达",@"sound",
                         nil ];
    VIZWarn *warn = [[VIZWarn alloc]initWithDic:dict];
    
    [self.warns addObject:warn];
    
//    [self.tableView reloadData];
    
}




//获取路径
- (NSString *)getFilePath{
    
    //    1.获取doc路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    //    2.拼接file路径
    NSString *filePath = [docPath stringByAppendingPathComponent:@"warns.data"];
    return filePath;
}


//写入数据
- (void)writeData{
    //    1.存储数据
    [NSKeyedArchiver archiveRootObject:self.warns toFile:[self getFilePath]];
    
}





@end
